new Vue({
    el: '#app',
    data() {
        return {
            IO_form: {
                Employees: [{
                    Employee_SerialNumber: "",
                    Employee_Name: "",
                    Employee_Phone: "",
                    organization: [],
                    Organization_SerialNumber: "",
                    Department_SerialNumber: "",
                    Enterprise_Info_SerialNumber: $.cookie("enterprise"),
                }],
            },
            rules: {
                Employee_Phone: [{
                    required: true,
                    message: "请输入手机号",
                    trigger: "blur"
                }, {
                    pattern: /^1[3456789]\d{9}$/,
                    message: '手机号码格式不正确',
                    trigger: 'blur'
                }],
            },
            Employees: [],
            loading: false,
            fileXlsx: [],
        }
    },
    mounted() {
        this.getEmployees()
    },
    methods: {
        AddEmployee() {
            this.IO_form.Employees.push({
                Employee_SerialNumber: "",
                Employee_Name: "",
                Employee_Phone: "",
                organization: '',
                Organization_SerialNumber: "",
                Department_SerialNumber: "",
                Enterprise_Info_SerialNumber: $.cookie("enterprise"),
            })
        },
        RdcEmployee() {
            if (this.IO_form.Employees.length > 1) {
                this.IO_form.Employees.pop()
            } else {
                this.$message({
                    message: '不可以再删除了!',
                    type: 'warning'
                });
            }
        },
        getEmployees() {
            const this_ = this
            $.ajax({
                url: DomainName + '/getOrganizationAction?token=' + $.cookie('token'),
                type: 'POST',
                data: {
                    Enterprise_Info_SerialNumber: $.cookie("enterprise"),
                },
                success: function (res) {
                    console.log(res)
                    this_.Employees = res.Data
                }
            })
        },
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    this.loading = true
                    const this_ = this
                    this_.IO_form.Employees.forEach(item => {
                        item.Department_SerialNumber = item.organization[1]
                        item.Organization_SerialNumber = item.organization[0]
                    })
                    $.ajax({
                        url: DomainName + '/getBatchAddAction?token=' + $.cookie('token'),
                        type: 'POST',
                        data: {
                            tableName: 'employee',
                            Data: JSON.stringify(this_.IO_form.Employees),
                            EncryptByAES:true,
                            Primary_Key_Name:'Employee_Phone',
                        },
                        success: function (res) {
                            console.log(res)
                            if (res.Status == 'success') {
                                this_.$message({
                                    message: '提交成功',
                                    type: 'success'
                                });
                                this_.back()
                            }
                        }
                    })
                } else {
                    this.$message({
                        message: '请检查表单信息是否有漏填或填写不正确!',
                        type: 'error'
                    });
                    return false;
                }
            });
        },
        uploadXLSX(file) {
            this.fileXlsx.push({
                name: file.name,
                url: file.type
            })
            const _this = this
            let arr = []
            var reader = new FileReader();
            reader.readAsBinaryString(file);
            reader.onload = function (e) {
                var data = e.target.result;
                var wb = XLSX.read(data, {
                    type: 'binary' //以二进制的方式读取
                });
                var sheet = wb.Sheets[wb.SheetNames[0]];//sheet0代表excel表格中的第一页
                var json = XLSX.utils.sheet_to_json(sheet);//利用接口实现转换。
                console.log(json);
                _this.IO_form.Employees = json
                var a = {
                    Employee_SerialNumber: "",
                    Employee_Name: "",
                    Employee_Phone: "",
                    organization: '',
                    Organization_SerialNumber: "",
                    Department_SerialNumber: "",
                    Enterprise_Info_SerialNumber: $.cookie("enterprise"),
                }
                _this.IO_form.Employees.forEach((item, i) => {
                    if (_this.Employees.some(key => key['text'] == item['所属组织'])) {
                        var res = _this.Employees.find(function (item_, index, array) {
                            return item_.text == item['所属组织'] ? item_ : false
                        })
                        if(res.children.some(key_=>key_['text']==item['所属部门'])){
                            var res1 = res.children.find(function (item_1, index, array) {
                                return item_1.text == item['所属部门'] ? item_1 : false
                            })
                        }else{
                            _this.$message.error('elex中的组织中有不存在的部门')
                        }
                        item.organization = [res.value, res1.value]
                        item.Employee_Name = ''
                        item.Employee_SerialNumber = ''
                        item.Enterprise_Info_SerialNumber = $.cookie("enterprise")
                    }else{
                        _this.$message.error('elex中有不存在的组织')
                    }
                    item.Employee_Phone = Number(item['员工手机号'])
                })
                console.log(_this.IO_form.Employees)
                // _this.total2 = _this.tableData1.length
            }
        },
        XlsxbeforeRemove() {
            this.fileXlsx.pop()
            this.IO_form.Employees = [{
                Employee_SerialNumber: "",
                Employee_Name: "",
                Employee_Phone: "",
                organization: [],
                Organization_SerialNumber: "",
                Department_SerialNumber: "",
                Enterprise_Info_SerialNumber: $.cookie("enterprise"),
            }]
        },
        back() {
            window.history.go(-1)
        }
    },
})